﻿using AutoMapper.Internal.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Himp.Platform
{
    /// <summary>
    /// 业务字典
    /// </summary>
    public class BizDictService : CrudAppService<BizDict, BizDictDto, Guid, BizDictInput, BizDictCreateDto, BizDictUpdateDto>, IBizDictService
    {
        private IRepository<BizDict, Guid> _BizDictRepository;

        public BizDictService(IRepository<BizDict, Guid> BizDictRepository) : base(BizDictRepository)
        {
            _BizDictRepository = BizDictRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<BizDictDto>> GetListAsync(BizDictInput input)
        {
            var iQuery = await _BizDictRepository.WithDetailsAsync();
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.CatgeCodg), b => b.CatgeCodg == input.CatgeCodg);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<BizDict>, List<BizDictDto>>(iQuery.ToList());

                return new PagedResultDto<BizDictDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<BizDict>, List<BizDictDto>>(items.ToList());

                return new PagedResultDto<BizDictDto>(totalCount, itemsDto);
            }
        }
    }
}
